using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Umdf.SideShow;

namespace SideShowClient
{
    public partial class ApplicationList : UserControl
    {
        private List<SideShowApplication> _applications;

        public event EventHandler SelectedApplicationChanged;

        public ApplicationList()
        {
            InitializeComponent();
        }

        public List<SideShowApplication> Applications
        {
            get { return _applications; }
            set
            {
                _applications = value;
                BindData();
            }
        }

        public SideShowApplication SelectedApplication
        {
            get
            {
                if (lvApplications.SelectedItems.Count == 1)
                {
                    return (SideShowApplication) lvApplications.SelectedItems[0].Tag;
                }
                return null;
            }
        }

        private void BindData()
        {
            lvApplications.Items.Clear();
            imlSmallImages.Images.Clear();
            foreach (SideShowApplication app in _applications)
            {
                ListViewItem newItem = new ListViewItem(app.Name, app.Id.ToString());
                newItem.Tag = app;
                imlSmallImages.Images.Add(app.Id.ToString(), app.SmallIcon);
                lvApplications.Items.Add(newItem);
            }
        }

        private void lvApplications_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedApplicationChanged != null)
            {
                SelectedApplicationChanged(this, e);
            }
        }

        private void lvApplications_Resize(object sender, EventArgs e)
        {
            columnHeader1.Width = lvApplications.ClientSize.Width;
        }
    }
}
